class GuideController < ApplicationController
  #权限验证
  before_filter :auth
  layout "admin"
  def index
    redirect_to :action=>'list'
  end

  def new
    @guide = Guide.new
    @titles = "新增导量"
  end


  def create
    @guide = Guide.new(params[:guide])    
    if  @guide && @guide.save
      GuideExec.set_guide_map @guide
      flash[:notice] = '搞定一个！'
      redirect_to :action=>'list'
    else
      render :action => "new"
    end
  end


  #查询
  def list
    #提交表单为post
    if request.post?
      @guide_search = params[:guide]
      name = @guide_search[:name]
      @guides = Guide.paginate :per_page => $perPage, :page =>  params[:page],
        :conditions => ['name like ?', "%#{name}%"], :order => 'created_at desc'
    else
      @guides = Guide.paginate :page => params[:page],
        :per_page => $perPage,        
        :order => "created_at desc"
    end
    #查询项参数
    @guide = Guide.new
    @guide.name = name
    #功能名
    @titles = "导量列表"
  end

  def edit
    @guide = Guide.find(params[:id])
    @titles = "编辑导量---"+@guide.name
  end

  def update
    @guide = params[:guide]    
    if Guide.update(@guide[:id], @guide)
      g = Guide.find(@guide[:id])
      GuideExec.set_guide_map g
      flash[:notice] = '更新成功'
      redirect_to :action=>'index'
    else
      render :action => "edit"
    end
  end

  def show
    @guide = Guide.find(params[:id])
    @titles = "频道导量信息---"+@guide.name
  end

  def delete
    guide = Guide.find(params[:id])
    guide_id = params[:id]
    ge = GuideExec.find :all,
      :conditions => ['guide_id = ?',guide_id],
      :order => ['created_at desc'],
      :limit => '1'    
    guide.destroy
    redirect_to :action=>'index'
  end
  

end
